﻿using Microsoft.Extensions.Configuration;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nucleic.Core.Utils
{
    public class SqlSugarFactory
    {
        private SqlSugarFactory() { }
        private static ISqlSugarClient instance;
        //静态只读对象用于辅助实现lock
        private static readonly object locker = new object();

        //public static ISqlSugarClient GetInstance()
        //{
        //    //首次检验：是否需要加锁
        //    if (instance == null)
        //    {
        //        //为当前线程加锁
        //        lock (locker)
        //        {
        //            //再次检验：避免当前线程实例化instance后，下一个线程再次实例
        //            if (instance == null)
        //                instance = new SqlSugarClient(new ConnectionConfig() { ConnectionString = ConfigurationUtil.GetSection("ConnectionStrings:Default"), DbType = DbType.SqlServer, IsAutoCloseConnection = true });
        //        }
        //    }
        //    return instance;
        //}
        public static ISqlSugarClient Instance
        {
            get{
                SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
                //调试SQL事件，可以删掉
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql);//输出sql,查看执行sql
                };
                return db;
            }
        }
    }
}
